//
//  MXMineVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/4.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 我的 首页 有 专题收藏 单品收藏 清除缓存 关于我们 设置等


#import "MXMineVC.h"
#import "UIButton+Init.h"
#import "UIView+Extension.h"
#import "MXMineTopicCollectVC.h"
#import "MXMineItemCollectionVC.h"
#import "SDImageCache.h"
#import "CPKenburnsView.h"

@interface MXMineVC ()<UIAlertViewDelegate>
@property (nonatomic, retain) NSString *string;

@property (nonatomic, retain) UIButton *CacheClearButton;

@property (nonatomic, retain) UIView *window;

/**
 * 判断当前是否是夜间模式
 */
@property (nonatomic, assign) BOOL isOpenNightModel;

@end

@implementation MXMineVC
{
    CPKenburnsView *kenbunrsView;
}
- (void)dealloc
{
    [_window release];
    [_CacheClearButton release];;
    [_string release];
    [super dealloc];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Home_titleBackGround"] forBarMetrics:UIBarMetricsDefault];
    
    kenbunrsView = [[CPKenburnsView alloc] initWithFrame:self.view.bounds];
    kenbunrsView.image = [UIImage imageNamed:@"Mine_background.JPG"];
    [self.view addSubview:kenbunrsView];
    [kenbunrsView release];
    [self creatButtons];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"我的";
    
    
    self.view.userInteractionEnabled = YES;
    
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];

    
    
    // Do any additional setup after loading the view.
}

- (void)creatButtons
{
    UIButton *topicCollectButton = [UIButton buttonWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 70, HEIGHTPRO * 150, WIDTHPRO * 140, HEIGHTPRO * 40) type:(UIButtonTypeCustom) title:@"专题收藏" target:nil action:nil];
    
    topicCollectButton.layer.cornerRadius = WIDTHPRO * 5;
    
    [topicCollectButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    topicCollectButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    topicCollectButton.layer.borderWidth = WIDTHPRO * 1;
    
    topicCollectButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    
    [topicCollectButton addTarget:self action:@selector(topicAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    topicCollectButton.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:topicCollectButton];
    
    
    UIButton *itemCollectionButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    itemCollectionButton.frame = CGRectMake(WIDTH / 2 - WIDTHPRO * 70, HEIGHTPRO * 210, WIDTHPRO * 140, HEIGHTPRO * 40);
    
    itemCollectionButton.layer.cornerRadius = WIDTHPRO * 5;
    
    itemCollectionButton.layer.borderWidth = WIDTHPRO * 1;
    
    itemCollectionButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [itemCollectionButton setTitle:@"单品收藏" forState:(UIControlStateNormal)];
    
    [itemCollectionButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    itemCollectionButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    
    itemCollectionButton.backgroundColor = [UIColor clearColor];
    
    [itemCollectionButton addTarget:self action:@selector(itemCollectionAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:itemCollectionButton];
    
    
    NSInteger size = [[SDImageCache sharedImageCache] getSize];
    
    self.string = [NSString stringWithFormat:@"清除缓存  %ldMB", (long)size / (1024 * 1024)];
    
    self.CacheClearButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.CacheClearButton.frame = CGRectMake(WIDTH / 2 - WIDTHPRO * 70, HEIGHTPRO * 270, WIDTHPRO * 140, HEIGHTPRO * 40);
    
    self.CacheClearButton.backgroundColor = [UIColor clearColor];
    
    self.CacheClearButton.layer.cornerRadius = WIDTHPRO * 5;
    
    self.CacheClearButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    self.CacheClearButton.layer.borderWidth = WIDTHPRO * 1;
    
    self.CacheClearButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    
    [self.CacheClearButton setTitle:self.string forState:(UIControlStateNormal)];
    
    [self.CacheClearButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    [self.CacheClearButton addTarget:self action:@selector(clearCacheBtnTouch) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.CacheClearButton];
    
    UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 70 + WIDTHPRO * 150, HEIGHTPRO * 332, WIDTHPRO * 140, HEIGHTPRO * 40)];
    
    if (self.isOpenNightModel == YES) {
        
        switchButton.on = YES;

    } else {
        switchButton.on = NO;

    }
    
    
    [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:(UIControlEventValueChanged)];
    
    [self.view addSubview:switchButton];
    
    UILabel  *nightLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 70, HEIGHTPRO * 330, WIDTHPRO * 140, HEIGHTPRO * 40)];
    
    nightLabel.text = @"夜间模式";
    
    nightLabel.textAlignment = NSTextAlignmentCenter;
    
    nightLabel.textColor = [UIColor whiteColor];
    
    nightLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    
    nightLabel.layer.borderWidth = 1;
    
    [self.view addSubview:nightLabel];
    
    
}

- (void)switchAction:(UISwitch *)sender
{
    
    [self.window removeFromSuperview];
    
    if (sender.isOn) {
        
        self.isOpenNightModel = YES;
        
        self.window = [[[UIView alloc] initWithFrame:self.view.window.bounds] autorelease];
        
        self.window.backgroundColor = [UIColor blackColor];
        
        self.window.alpha = 0.5;
        
        self.window.userInteractionEnabled = NO;
        
        [self.view.window addSubview:self.window];
        
    } else {
        self.isOpenNightModel = NO;
        
        [self.window removeFromSuperview];
    }
}


-(void)clearCacheBtnTouch
{
    UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要清除所有缓存么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [al show];
    [al release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (1 == buttonIndex) {
        //NSInteger size = [[SDImageCache sharedImageCache] getSize];
        [[SDImageCache sharedImageCache] clearDisk];
        self.string = [NSString stringWithFormat:@"清除缓存   0 MB"];
        [self.CacheClearButton setTitle:self.string forState:UIControlStateNormal];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)topicAction
{
    MXMineTopicCollectVC *topicCollectVC = [[[MXMineTopicCollectVC alloc] init] autorelease];
    [self.navigationController pushViewController:topicCollectVC animated:YES];
}
- (void)itemCollectionAction
{
    MXMineItemCollectionVC *itemVC = [[[MXMineItemCollectionVC alloc] init] autorelease];
    [self.navigationController pushViewController:itemVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
